package cn.yuyin.item;

import java.io.IOException;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import cn.yuyin.domain.FriendBean;
import cn.yuyin.jiaoyou.page.ChatPanel;
import cn.yuyin.main.Main;

public class ChatItem extends Item{
	private Image mail,phone;
	private TextItem textItem = null;
	private String date;
	private FriendBean fb;
	
	public ChatItem(int index,int x, int y, String name,String Content,String date) {
		super(x, y, name);
		this.index = index;
		this.date = date;
		fb = null;
		textItem = new TextItem(x, y+Main.font2.getHeight()+2*Main.rowspace, Content,Main.WIDTH-3*Main.margin, Main.font2, 0);
		width = Main.WIDTH;
		height = Main.font.getHeight()+textItem.height+Main.rowspace;
		try {
			mail = Image.createImage("/mail.png");
			phone = Image.createImage("/phone.png");
		} catch (IOException e) {
			e.printStackTrace();
		}
		endX = x;
		endY = textItem.endY+Main.font.getHeight();
	}
	
	public ChatItem(int index,int x, int y, String name,String Content,String date,FriendBean fb) {
		super(x, y, name);
		this.index = index;
		this.date = date;
		this.fb=fb;
		textItem = new TextItem(x, y+Main.font2.getHeight()+2*Main.rowspace, Content,Main.WIDTH-3*Main.margin, Main.font2, 0);
		width = Main.WIDTH;
		height = 2*Main.font.getHeight()+textItem.height+Main.rowspace;
		try {
			mail = Image.createImage("/mail.png");
			phone = Image.createImage("/phone.png");
		} catch (IOException e) {
			e.printStackTrace();
		}
		endX = x;
		endY = textItem.endY+2*Main.font.getHeight()+Main.rowspace;
	}

	public void paint(Graphics g) {
		if(index == ChatPanel.cnumber){
			ishaveFouse = true;
		}else {
			ishaveFouse = false;
		}
		int color = g.getColor();
		
		if(ishaveFouse){
			g.setColor(0x5F8FFF);
			switch (ChatPanel.hnumber) {
			case 0:
				g.drawRect(x-1, y-1, Main.WIDTH + 2 - (x << 1), height+1);
				break;
			case 1:
				g.drawRect(Main.WIDTH-2*mail.getWidth()-3*Main.rowspace-1, y-1, phone.getWidth()+2, phone.getHeight()+2);
				break;
			case 2:
				g.drawRect(Main.WIDTH-mail.getWidth()-2*Main.rowspace-1, y-1, mail.getWidth()+2, mail.getHeight()+2);
				break;
			default:
				break;
			}
			g.setColor(0);
			g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
			if(text.equals(Main.name)){
				g.setColor(0x008040);
			}else{
				g.setColor(0x0000ff);
			}
			g.drawString(text+"  "+date, x, y, 0);
			g.setColor(0);
			g.drawImage(mail, Main.WIDTH-mail.getWidth()-2*Main.rowspace, y, 0);
			g.drawImage(phone, Main.WIDTH-2*mail.getWidth()-3*Main.rowspace, y, 0);
			g.setFont(Main.font);
			textItem.y = y+Main.font.getHeight()+Main.rowspace;
			textItem.paint(g);
			if(fb!=null){
				g.setColor(0x0000ff);
				g.drawString("("+fb.getDistance()+")", Main.WIDTH-2*Main.margin-Main.font.stringWidth("("+fb.getDistance()+")"), textItem.y+textItem.height, 0);
			}			
		}else{
			g.setColor(0);
			g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
			if(text.equals(Main.name)){
				g.setColor(0x008040);
			}else{
				g.setColor(0x0000ff);
			}
			g.drawString(text+"  "+date, x, y, 0);
			g.setColor(0);
			g.drawImage(mail, Main.WIDTH-mail.getWidth()-2*Main.rowspace, y, 0);
			g.drawImage(phone, Main.WIDTH-2*mail.getWidth()-3*Main.rowspace, y, 0);
			g.setFont(Main.font);
			textItem.y = y+Main.font.getHeight()+Main.rowspace;
			textItem.paint(g);
			if(fb!=null){
				g.setColor(0x0000ff);
				g.drawString("("+fb.getDistance()+")", Main.WIDTH-2*Main.margin-Main.font.stringWidth("("+fb.getDistance()+")"), textItem.y+textItem.height, 0);
			}			
		}
		g.setColor(color);
	}
	public FriendBean getFb() {
		return fb;
	}
	public void setFb(FriendBean fb) {
		this.fb = fb;
	}
	public void keyReleased(int keyCode) {
		
	}
	public void pointerPressed(int x, int y) {
		
	}
	public void pointerReleased(int x, int y) {
		
	}
}
